<template>
	<view>
	</view>
</template>

<script>
	import {
		couponUseCoupon
	} from '@/api/merchant/coupon.js'
	import permission from '@/utils/permission.js'
	export default {
		data() {
			return {
				subnvue: null
			};
		},
		computed: {

		},
		onLoad(option) {
			uni.$on('coupon-scan', async e => {
				if (e.type === 'setting') {
					console.log(e)
					let { paltform } = uni.getSystemInfoSync()
					if (paltform == 'ios') {
						let result = await permission.judgeIosPermission('camera')
					} else {
						let result = await permission.requestAndroidPermission('android.permission.CAMERA')
						if (result === -1) {
							permission.gotoAppPermissionSetting()
						}
						console.log(result)
					}
				} else {
					this.jumpUrl(`/pages/shop/coupon/search/index/index`)
				}
			})
			// #ifdef APP-PLUS
			//扫超过30秒无法识别出优惠券，弹窗引导进行手动输入；点击确认进行手动输入，点击取消关闭弹窗继续扫码
			this.subnvue = uni.getSubNVueById('app-scan') // 获取nvue
			this.subnvue.show() // 显示nvue
			let timer2 = setTimeout(() => {
				clearTimeout(timer2)
				uni.$emit('coupon-msg', ({
					type: 'text',
					msg: this.lang_shop.inputCouponTips,
					btnText: this.lang_shop.inputCoupon
				}))
			}, 300)
			uni.$on('hs-scancode', e => {
				let {
					result,
					errMsg
				} = e[0] || {}
				if (errMsg === 'scanCode:ok') {
					this.dealResult(result)
				}
			})
			// #endif
		},
		onUnload() {
			this.subnvue.close()
			this.subnvue = null
		},
		methods: {
			// 处理nvue组件的传输的内容
			dealResult(result) {
				couponUseCoupon({
					couponSn: result.slice(3, -3)
				}).finally((data) => {
					setTimeout(() => {
						uni.$emit('coupon-msg', ({ type: 'event', value: 'init' }))
					}, 2000)
				})
			}
		},
	};
</script>

<style>
	.cam-container {
		width: 100%;
		height: 300px;
		margin-bottom: 10rpx;
	}
</style>
